package com.maxwell.googleplay.fragment;

import java.util.List;
import java.util.Random;

import com.maxwell.googleplay.base.BaseFragment;
import com.maxwell.googleplay.base.LoadingPager.LoadedResult;
import com.maxwell.googleplay.protocol.RecommendProtocol;
import com.maxwell.googleplay.utils.UIUtils;
import com.maxwell.googleplay.views.flyoutin.ShakeListener;
import com.maxwell.googleplay.views.flyoutin.ShakeListener.OnShakeListener;
import com.maxwell.googleplay.views.flyoutin.StellarMap;

import android.R.color;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @author yue
 * @创建时间 2016-3-30上午9:26:27
 * @描述 TODO
 * 
 */
public class RecommendFragment extends BaseFragment {

	private List<String> mDatas;
	private ShakeListener mShakeListener;

	@Override
	public LoadedResult initData() {
		RecommendProtocol protocol = new RecommendProtocol();
		try {
			mDatas = protocol.loadData(0);
			checkState(mDatas);
			return LoadedResult.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return LoadedResult.ERROR;
		}
	}

	@Override
	public View initSuccessView() {
		//
		final StellarMap stellarMap = new StellarMap(UIUtils.getContext());
		
		
		
		//设置adapter
		final RecommendAdapter adapter = new RecommendAdapter();
		stellarMap.setAdapter(adapter);
		
		
		//设置第一页的时候显示
		stellarMap.setGroup(0, true);
		//设置把屏幕拆分成多少格子
		stellarMap.setRegularity(15, 20);
		
		//加入摇一摇切换
		mShakeListener = new ShakeListener(UIUtils.getContext());
		mShakeListener.setOnShakeListener(new OnShakeListener() {
			
			@Override
			public void onShake() {
				//
				int groupIndex = stellarMap.getCurrentGroup();
				if(groupIndex == adapter.getGroupCount() - 1){
					groupIndex = 0;
				}else{
					groupIndex ++;
				}
				groupIndex ++;
				stellarMap.setGroup(groupIndex, true);
			}
		});
		
		
		return stellarMap;
	}
	
	@Override
	public void onResume() {
		if(mShakeListener != null){
			mShakeListener.resume();
		}
		super.onResume();
	}
	
	@Override
	public void onPause() {
		if(mShakeListener != null){
			mShakeListener.pause();
		}
		super.onPause();
	}
	
	class RecommendAdapter implements StellarMap.Adapter{

		private static final int PAGER_SIZE = 15;//每页显示多少条数据
		
		@Override
		public int getGroupCount() {//有多少组
			//
			int groupCount = mDatas.size()/PAGER_SIZE;
			if(mDatas.size() % PAGER_SIZE > 0){
				groupCount ++;
			}
			
			return groupCount;
		}

		@Override
		public int getCount(int group) {//每组有多少条数据
			//
			if((group == getGroupCount() - 1) && (mDatas.size() % PAGER_SIZE > 0)){
				return mDatas.size() % PAGER_SIZE;
			}
				
			return PAGER_SIZE;
		}

		/* group:第几组
		 * 
		 * position:某组中的第几个位置
		 * 
		 */
		@Override
		public View getView(int group, int position, View convertView) {//返回具体的View

			TextView tv = new TextView(UIUtils.getContext());
			int index = group * PAGER_SIZE + position;
			
			tv.setText(mDatas.get(index));
			int padding = UIUtils.dip2px(10);
			tv.setPadding(padding, padding, padding, padding);
			
			//设置随机大小
			Random random = new Random();
			tv.setTextSize(random.nextInt(10) + 10);
			
			//设置随机颜色			
			int alpha = 255;
			int red = random.nextInt(180) + 30;
			int green = random.nextInt(180) + 30;
			int blue = random.nextInt(180) + 30;			
			int  argb = Color.argb(alpha, red, green, blue);
			tv.setTextColor(argb );
			
		
			
			return tv;
		}

		@Override
		public int getNextGroupOnPan(int group, float degree) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public int getNextGroupOnZoom(int group, boolean isZoomIn) {
			// TODO Auto-generated method stub
			return 0;
		}
		
	}
}
